#include "DarkTitleBar.h"

#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QEvent>
#include <QMouseEvent>
#include <QApplication>

#ifdef Q_OS_WIN
#pragma comment(lib, "user32.lib")
#include <qt_windows.h>
#endif

DarkTitleBar::DarkTitleBar(QWidget *parent)
	: QWidget(parent)
{
	setFixedHeight(40);
    setMinimumWidth(200);
    setFocusPolicy(Qt::FocusPolicy::StrongFocus);
	m_pIconLabel = new QLabel(this);
	m_pTitleLabel = new QLabel(this);
	m_pMinimizeButton = new QPushButton(this);
	m_pMaximizeButton = new QPushButton(this);
	m_pCloseButton = new QPushButton(this);

    m_pMinimizeButton->installEventFilter(this);
    m_pMaximizeButton->installEventFilter(this);
    m_pCloseButton->installEventFilter(this);

	m_pIconLabel->setFixedSize(28, 28);
	m_pIconLabel->setScaledContents(true);

	m_pTitleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
	QPalette pe;
	pe.setColor(QPalette::WindowText, QColor(210,210,210));
	m_pTitleLabel->setPalette(pe);
	
	QFont font("Microsoft YaHei", 9);
	m_pTitleLabel->setFont(font);

	m_pMinimizeButton->setFixedSize(20, 20);
	m_pMaximizeButton->setFixedSize(20, 20);
	m_pCloseButton->setFixedSize(20, 20);

	m_pTitleLabel->setObjectName("whiteLabel");
	m_pMinimizeButton->setObjectName("minimizeButton");
	m_pMaximizeButton->setObjectName("maximizeButton");
	m_pCloseButton->setObjectName("closeButton");

	m_pMinimizeButton->setToolTip("Minimize");
	m_pMaximizeButton->setToolTip("Maximize");
	m_pCloseButton->setToolTip("Close");
	//QIcon(":/image/cgis-w.ico")
    QIcon ic("./image/cgis-w.png");
    m_pIconLabel->setPixmap(ic.pixmap(m_pIconLabel->size()));
	m_pMinimizeButton->setIcon(QIcon(":/images/icon_window_minimize.png"));
	m_pMaximizeButton->setIcon(QIcon(":/images/icon_window_maximize.png"));
	m_pCloseButton->setIcon(QIcon(":/images/icon_window_close.png"));

	m_pMinimizeButton->setFlat(true);
	m_pMinimizeButton->setStyleSheet("QPushButton{background: transparent;}");
	m_pMaximizeButton->setFlat(true);
    m_pMaximizeButton->setMouseTracking(true);
	m_pMaximizeButton->setStyleSheet("QPushButton{background: transparent;}");
    m_pCloseButton->setFlat(true);
    m_pCloseButton->setMouseTracking(true);
	m_pCloseButton->setStyleSheet("QPushButton{background: transparent;}");

	QHBoxLayout *pLayout = new QHBoxLayout(this);
	pLayout->addWidget(m_pIconLabel);
	pLayout->addSpacing(5);
	pLayout->addWidget(m_pTitleLabel);
	pLayout->addWidget(m_pMinimizeButton);
	pLayout->addWidget(m_pMaximizeButton);
	pLayout->addWidget(m_pCloseButton);
	pLayout->setSpacing(5);
	pLayout->setContentsMargins(5, 0, 5, 0);

	QPalette pal(this->palette());
	//pal.setColor(QPalette::Background, QColor(44, 44, 44));
	pal.setColor(QPalette::Background, QColor(38, 102, 173, 50));
	this->setAutoFillBackground(true);
	this->setPalette(pal);

	setLayout(pLayout);

	connect(m_pMinimizeButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
	connect(m_pMaximizeButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
	connect(m_pCloseButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
}

DarkTitleBar::~DarkTitleBar()
{

}

void DarkTitleBar::setBackColor(QColor c)
{
	QPalette pal(this->palette());
	pal.setColor(QPalette::Background, c);
	this->setAutoFillBackground(true);
	this->setPalette(pal);
}

void DarkTitleBar::setMainWindowMode()
{
	setFixedHeight(40);

	m_pTitleLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

	//QFont font("Microsoft YaHei", 16, 66);
	QFont font("SimHei", 20);
	//font.setStyleHint(QFont::Courier);
	//font.setStyleStrategy(QFont::PreferBitmap);
	m_pTitleLabel->setFont(font);

	QPalette pe;
	pe.setColor(QPalette::WindowText, QColor(230, 230, 230));
	m_pTitleLabel->setPalette(pe);

    m_pIconLabel->setFixedSize(28, 28);
	m_pMinimizeButton->setFixedSize(30, 30);
	m_pMaximizeButton->setFixedSize(30, 30);
	m_pCloseButton->setFixedSize(30, 30);
}

void DarkTitleBar::setContent(QWidget * w)
{
}

void DarkTitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
	Q_UNUSED(event);

	emit m_pMaximizeButton->clicked();
}

void DarkTitleBar::mousePressEvent(QMouseEvent *event)
{
#ifdef Q_OS_WIN
	if (ReleaseCapture())
	{
		QWidget *pWindow = this->window();
		if (pWindow->isTopLevel())
		{
			SendMessage(HWND(pWindow->winId()), WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
		}
	}
	event->ignore();
#else
#endif
}

bool DarkTitleBar::eventFilter(QObject *obj, QEvent *event)
{
    //if (obj == m_pMinimizeButton) {
    //    if (event->type() == QEvent::Enter) {
    //        m_pMinimizeButton->setIcon(QIcon(":/images/icon_window_minimize.png"));
    //    }
    //    else if (event->type() == QEvent::Leave) {
    //        m_pMinimizeButton->setIcon(QIcon(":/images/icon_window_minimize.png"));
    //    }
    //}
    //if (obj == m_pMaximizeButton) {
    //    if (event->type() == QEvent::Enter) {
    //        m_pMaximizeButton->setIcon(QIcon(":/images/icon_window_maximize.png"));
    //    }
    //    else if (event->type() == QEvent::Leave) {
    //        m_pMaximizeButton->setIcon(QIcon(":/images/icon_window_maximize.png"));
    //    }
    //}
    //if (obj == m_pCloseButton) {
    //    if (event->type() == QEvent::Enter) {
    //        m_pCloseButton->setIcon(QIcon(":/images/icon_window_close.png"));
    //    }
    //    else if (event->type() == QEvent::Leave) {
    //        m_pCloseButton->setIcon(QIcon(":/images/icon_window_close.png"));
    //    }
    //}
	switch (event->type())
	{
	case QEvent::WindowTitleChange:
	{
		QWidget *pWidget = qobject_cast<QWidget *>(obj);
		if (pWidget)
		{
			m_pTitleLabel->setText(pWidget->windowTitle());
			return true;
		}
	}
	case QEvent::WindowIconChange:
	{
		QWidget *pWidget = qobject_cast<QWidget *>(obj);
		//if (pWidget)
		//{
		//	QIcon icon = pWidget->windowIcon();
		//	m_pIconLabel->setPixmap(icon.pixmap(m_pIconLabel->size()));
		//	return true;
		//}
	}
	case QEvent::WindowStateChange:
	case QEvent::Resize:
		updateMaximize();
		return true;
	}
	return QWidget::eventFilter(obj, event);
}

void DarkTitleBar::onClicked()
{
	QPushButton *pButton = qobject_cast<QPushButton *>(sender());
	QWidget *pWindow = this->window();
	//if (pWindow->isTopLevel())
	{
		if (pButton == m_pMinimizeButton)
		{
			pWindow->showMinimized();
		}
        else if (pButton == m_pMaximizeButton)
        {
            //pWindow->showMaximized();
            //pWindow->isMaximized() ? {pWindow->showNormal(); } : {pWindow->showMaximized(); };
            if (pWindow->isMaximized()) {
                pWindow->showNormal();
                m_pMaximizeButton->setIcon(QIcon(":/images/icon_window_maximize.png"));
            }
            else {
                pWindow->showMaximized();
                m_pMaximizeButton->setIcon(QIcon(":/images/icon_window_restore.png"));
            }
		}
		else if (pButton == m_pCloseButton)
		{
			pWindow->close();
		}
	}
}

void DarkTitleBar::updateMaximize()
{
	QWidget *pWindow = this->window();
	if (pWindow->isTopLevel())
	{
		bool bMaximize = pWindow->isMaximized();
		if (bMaximize)
		{
			m_pMaximizeButton->setToolTip(tr("Restore"));
			m_pMaximizeButton->setProperty("maximizeProperty", "restore");
		}
		else
		{
			m_pMaximizeButton->setProperty("maximizeProperty", "maximize");
			m_pMaximizeButton->setToolTip(tr("Maximize"));
		}

		m_pMaximizeButton->setStyle(QApplication::style());
	}
}
